b039c3
@@ -48,6 +48,7 @@
 import org.apache.hadoop.fs.FsShell;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.fs.PathFilter;
+import org.apache.hadoop.fs.permission.FsPermission;
 import org.apache.hadoop.hive.common.FileUtils;
 import org.apache.hadoop.hive.common.HiveStatsUtils;
 import org.apache.hadoop.hive.common.ObjectPair;
@@ -2278,9 +2279,26 @@
static protected void copyFiles(HiveConf conf, Path srcf, Path destf,
     try {
       // create the destination if it does not exist
       if (!fs.exists(destf)) {
-        fs.mkdirs(destf);
         if (inheritPerms) {
-          fs.setPermission(destf, fs.getFileStatus(destf.getParent()).getPermission());
+          //need to find last existing path, and apply its permission on entire subtree.
+          Path path = destf;
+          List<Path> pathsToSet = new ArrayList<Path>();
+          while (!fs.exists(path)) {
+            pathsToSet.add(path);
+            path = path.getParent();
+          }
+
+          //at the end of this loop, path is the last existing path (the real parent).
+          fs.mkdirs(destf);
+          FsPermission parentPerm = fs.getFileStatus(path).getPermission();
+          for (Path pathToSet : pathsToSet) {
+            LOG.info("setting permission of parent folder: " + path.toString() +
+              " on new directory: " + pathToSet.toString());
+            fs.setPermission(pathToSet, parentPerm);
+          }
+        } else {
+          //simply make the directory.
+          fs.mkdirs(destf);
         }
       }
     } catch (IOException e) {
